import {createRouter, createWebHashHistory, createWebHistory, useRoute} from 'vue-router'

import Home from '@/pages/home.vue'
import Team from '@/pages/team.vue'
import User from '@/pages/user.vue'
import Searchpage from "@/pages/Searchpage.vue";
import Editpage from "@/pages/editpage.vue";
import SearchResult from "@/pages/SearchResult.vue";
import Login from "@/pages/login.vue";
import Register from "@/pages/register.vue";
import {userStore} from "../store/userStore.ts";
import {getUserInfo} from "../api/user.ts";
import {getCookie} from "../utils";
import TeamAddPage from "../pages/teamAddPage.vue";
import SearTeamResult from "../pages/SearTeamResult.vue";
import mycreateTeam from "../pages/mycreateTeam.vue";
import myjoinTeam from "../pages/myjoinTeam.vue";
import tagSelect from "../pages/tagSelect.vue"


const routes = [
    { path: '/home', component: Home,name:"首页" },
    { path: '/team',  component: Team ,name:"找队伍"},
    { path: '/user', component: User,name: "个人信息"},
    { path: '/Searchpage',component: Searchpage ,name: "搜索用户"},
    {path: '/editpage', component: Editpage ,name: "编辑信息"},
    {path: '/SearchResult', component: SearchResult ,name:"搜索结果"},
    {path: '/login', component: Login},
    {path: '/register', component: Register},
    {path: '/',redirect: '/login'},
    {path: '/teamAdd' ,component: TeamAddPage,name: "创建队伍"},
    {path: '/searchTeam' ,component: SearTeamResult ,name: "搜索队伍"},
    {path: '/mycreateTeam',component: mycreateTeam ,name: "我创建的队伍"},
    {path: '/myJoinTeam',component: myjoinTeam,name: "我加入的队伍"},
    {path: '/tagSelect',component: tagSelect,name: "标签选择"}
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})


router.beforeEach(async (to, from, next) => {
    const whitelist = ['/login', '/register'];

    const id = 'JESSIONID';
    if(!whitelist.includes(to.path)){
        var cookie = getCookie();
        if(cookie){
            next()
        }
        else{
            next('/login');
        }
    }
    else {
        next();
    }
})
export default router